package top.healthylife.gateway.platform.eye.api.check;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.healthylife.basics.utils.exception.BusinessException;
import top.healthylife.gateway.core.config.BaseConfig;
import top.healthylife.gateway.core.service.CheckSecretService;
import top.healthylife.gateway.platform.eye.api.api.sifafengxian.EyeCourtApi;
import top.healthylife.gateway.platform.eye.api.config.SystemConfig;
import top.healthylife.gateway.platform.eye.api.util.OutHttpUtil;

import java.util.Arrays;

/**
 * @author : GaoZeXi
 * @date 2021/3/19 14:48
 */
@Service
@Slf4j
public class CheckSecretServiceImpl implements CheckSecretService {
    /**
     * 使用随机一个api进行账号测试，无需入参，查看三方账号的返回结果
     *
     * @param baseConfig
     */
    @Override
    public void checkSecret(BaseConfig baseConfig) {
        SystemConfig eyeConfig = (SystemConfig) baseConfig;
        String token = eyeConfig.getToken();
        String respContext = HttpRequest.get(EyeCourtApi.URL)
                .header("Authorization", token).execute().body();
        JSONObject jsonObject;
        jsonObject = JSONObject.parseObject(respContext);
        String errorCode = jsonObject.get("error_code").toString();
        if (!OutHttpUtil.ERROR_MAP.containsKey(errorCode)) {
            log.error("天眼查账号测试不通过,账号token:{},测试接口:{},errorCode:{}", token,"历史法院公告[873]",errorCode);
            throw new BusinessException("账号测试不通过,errorCode:{}",errorCode);
        }
        if (Arrays.asList(OutHttpUtil.TOKEN_WRONG).contains(errorCode)) {
            throw new BusinessException(OutHttpUtil.ERROR_MAP.get(errorCode));
        }
        log.info("天眼查账号测试通过,账号token:{}",token);
    }
}

